<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*v健 接口 第二版接口 api*/
class VJianApi extends  BaseApi
{
	
	//进出门禁
	const GetContractInfo="Wx/Ocmcom/Api/getVipuserInfo";// 获取会员信息
	const CheckIn="Wx/Ocmcom/Api/enter";//进场
	const ConfirmCheckOut="Wx/Ocmcom/Api/depart"; //出场
	
	//私教打卡
	const GetPTEmployeeList="wx/ocmcom/api/GetPTEmployeeList";// 私教列表
	const GetReservationList="wx/ocmcom/api/GetReservationList";//预约列表
	const DoLessonStart="wx/ocmcom/api/DoLessonStart"; //上课
	const DoLessonEnd="wx/ocmcom/api/DoLessonEnd"; //下课
	const CardList="wx/ocmcom/api/CardList"; //私教卡列表
	
//	//进出门禁
//	const GetContractInfo="https://dev.newugo.cn/Wx/Ocmcom/Api/getVipuserInfo";// 获取会员信息
//	const CheckIn="https://dev.newugo.cn/Wx/Ocmcom/Api/enter";//进场
//	const ConfirmCheckOut="https://dev.newugo.cn/Wx/Ocmcom/Api/depart"; //出场
//	
//	//私教打卡
//	const GetPTEmployeeList="https://dev.newugo.cn/wx/ocmcom/api/GetPTEmployeeList";// 私教列表
//	const GetReservationList="https://dev.newugo.cn/wx/ocmcom/api/GetReservationList";//预约列表
//	const DoLessonStart="https://dev.newugo.cn/wx/ocmcom/api/DoLessonStart"; //上课
//	const DoLessonEnd="https://dev.newugo.cn/wx/ocmcom/api/DoLessonEnd"; //下课
//	const CardList="https://dev.newugo.cn/wx/ocmcom/api/CardList"; //私教卡列表
	
	protected $shop_id;//门店id
	protected $appId;//商家编号
	protected $deviceId;//设备id
	protected $clubId;//门店id
	protected $url;//访问前缀
	
	/**
	 * 测试地址
	 * */
	 public function _initialize()
    {
//  	parent::_initialize();
    }
	/////////////////////////////////////////获取微健数据////////////////////////////////////////////////////////////////
	/**
	 * 方法前操作获取店铺信息
	 */
	function before_method($shop_id){
		//$config_shopinfo=cache("ShopInfo_".$shop_id,null);
		//$config=cache("ExtraConfig_".$shop_id,null);
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
		/**
		 * 店铺信息
		 * */
		$config_shopinfo=cache("ShopInfo_".$shop_id);
		if(empty($config_shopinfo)){
			$config_shopinfo=db("Shop")->where("id=".$shop_id)->find();
			if(empty($config_shopinfo)){
				return $this->ajax_error('','',"店铺id 不存在");
			}else{
				cache("ShopInfo_".$shop_id,$config_shopinfo);
			}
		}
		if($config_shopinfo['api_version']=='vjian'){// 微健系统
			$config=cache("ExtraConfig_".$shop_id);
	    	if(empty($config)){
				 $config=db("Shop")->where("id=".$shop_id." and api_version='vjian'")->value("extra_config");
				 if(empty($config)){
				 	$this->ajax_error('','',"店铺id配置错误,或者微健配置代码错误");
				}
				 $config=json_decode($config,TRUE);
				 cache("ExtraConfig_".$shop_id,$config);
	    	} 
			if(!isset($config['appId'])){
				$this->ajax_error('','',"未配置微健代码");
			}
	    	$this->appId=$config['appId'];
			$this->deviceId=$config['deviceId'];
			$this->clubId=$config['clubId'];
			$this->url=$config['url'];
		}		
	}
	/**
	 * 获取合同号
	 * @param $card_no
	 * @return $data
	 * [
	 *	    {
	 *		        "ContractID": "06162ef0-7022-433e-80bd-7d9d44dc113c",
	 *		        "MemberID": "5c5313c8-aa5b-493a-8664-88e89b19a6ca",
	 *		        "MemberName": "ERCC",
	 *		        "Sex": "鐢�",
	 *		        "Mobile": "18523020102",
	 *		        "CardNo": "2238082314",
	 *		        "StartDate": "2017-10-19",
	 *		        "EndDate": "2018-10-28",
	 *		        "ContractStatus": 1,
	 *		        "MembershipType": 2,
	 *		        "MembershipTypeName": "骞存湀鍗�",
	 *		        "SurplusNum": "0",
	 *		        "StoredSumValue": "0.00"
	 *		    }
	 *		]
	 */
	function GetContractInfo($card_no,$shop_id){
		$this->before_method($shop_id);
		try{
			$result= http_post($this->url.self::GetContractInfo,['appId'=>$this->appId,"deviceId"=>$this->deviceId,"ClubID"=>$this->clubId,"CardNo"=>$card_no]);
			if(!$result['flag']){
				return template_error($result['msg']);
			 }
			$result=json_decode($result['data'],TRUE);
			if($result['Status']==1){
				$result=$result['data'];
				return template_success('成功(微健)',array(
															  "ContractID"=>"",
															  "MemberID"=>$result['MemberID'],
															  "MemberName"=>$result['MemberName'],
															  "EndDate"=>"",
															  "StartDate"=>$result["StartDate"],
															  "ContractStatus"=>1,
															  "Sex"=>"",
															  "Mobile"=>$result['Mobile'],
															  "CardNo"=>$card_no,
															  "MembershipType"=>2,
															  "MembershipTypeName"=>"",
															  "SurplusNum"=>0,
															  "StoredSumValue"=>0,
															  
				));
			}else{
				return template_error($result['msg']);
			}
		}catch(Exception $e){
			return template_error("微键服务器异常:".$e->getMessage());
		}
	}
	/**
	 * 入场操作
	 * @param $card_no
	 */
	function CheckIn($card_no,$shop_id){
		$this->before_method($shop_id);
		try{
			$result= http_post($this->url.self::CheckIn,['appId'=>$this->appId,"deviceId"=>$this->deviceId,"ClubID"=>$this->clubId,"CardNo"=>$card_no]);
			if(!$result['flag']){
				return template_error($result['msg']);
			 }
			$result=json_decode($result['data'],TRUE);
			if($result['Status']==1){
				return template_success("成功(微健)",'');
			}else{
				return template_error($result['msg']);
			}
		}catch(Exception $e){
			return template_error("微键服务器异常:".$e->getMessage());
		}
	}
	/**
	 * 出场操作
	 * @param $card_no
	 */
	function ConfirmCheckOut($card_no,$shop_id){
		$this->before_method($shop_id);
		try{
			$result= http_post($this->url.self::ConfirmCheckOut,['appId'=>$this->appId,"deviceId"=>$this->deviceId,"ClubID"=>$this->clubId,"CardNo"=>$card_no]);
			if(!$result['flag']){
				return template_error($result['msg']);
			 }
			$result=json_decode($result['data'],TRUE);
			if($result['Status']==1){
				return template_success('成功(微健)','');
			}else{
				return template_error($result['msg']);
			}
		}catch(Exception $e){
			return template_error("微键服务器异常:".$e->getMessage());
		}
	}
	////////////////////////////////////私教打卡///////////////////////////////////////////////

	/**
	 * 私教获取
	 * http://SmartGYM.com/api/VJianApi/GetPTEmployeeList/shop_id/81
	 * */
	public  function GetPTEmployeeList($shop_id){
		$this->before_method($shop_id);
		$config_shopinfo=cache("ShopInfo_".$shop_id);
		if($config_shopinfo['api_version']=='vjian'){//微键系统
			$result= http_post($this->url.self::GetPTEmployeeList,['ClubID'=>$this->clubId]);
			if(!$result['flag']){
				return template_error($result['msg']);
			}
			$result=json_decode($result['data'],TRUE);
			if($result['Status']!=1){
				return $this->ajax_error('','0003','配置错误或者私教列表为空');
			}else{
				//微键的教练
				$pt_list= $result['data'];
				if(empty($pt_list)){
					return $this->ajax_success([]);
				}
				//录入模板了的教练
				$pt_sync=db("Interface")->where("shop_id=$shop_id and owner_type=2 and card_no in(select card_no from m_face_code where shop_id=$shop_id)")->column("card_no");
				foreach ($pt_list as $key => $value) {
					 if(!in_array($value['EmployeeCardNo'],$pt_sync)){
					 	unset($pt_list[$key]);
					 }
				}
				return $this->ajax_success(array_values($pt_list));
			}
		}else{
			return $this->ajax_error('','0003','api_version 暂不支持');
		}
	}
	
	/**
	 * 获取预约课程列表
	 * http://SmartGYM.com/api/VJianApi/GetReservationList/shop_id/7/EmployeeID/AE833CA4-16C9-4B5C-B666-39BD4AE7413C/EmployeeCardNo/0723109292
	 * */
	 public function GetReservationList($shop_id,$EmployeeID,$EmployeeCardNo){
	 	$this->before_method($shop_id);
		$config_shopinfo=cache("ShopInfo_".$shop_id);
		if(empty($EmployeeID)||empty($EmployeeCardNo)){
	 			return $this->ajax_error("","0003","EmployeeID,EmployeeCardNo 卡号 为空");
		}
		$count=db("Interface")->where(" shop_id=$shop_id and owner_type=2 and card_no='".input("EmployeeCardNo")."'")->count(1);
		if($count<1){
			return $this->ajax_error("","0003","pt未录入系统或未录入特征模板！");
		}
		if($config_shopinfo['api_version']=='vjian'){//微键系统
		 	$result= http_post($this->url.self::GetReservationList,['ClubID'=>$this->clubId,'EmployeeID'=>$EmployeeID]);
			if(!$result['flag']){
				return template_error($result['msg']);
			}
			$result=json_decode($result['data'],TRUE);
			if($result['Status']!=1){
				return $this->ajax_error('','0003','配置错误或者私教列表为空');
			}else{
				return $this->ajax_success($result['data']);
			}
		}else{
			return $this->ajax_error('','0003','无效的api_version');
		}
	 }
	 /**
	  * 获取上课教练的指纹和上课会员的指纹
	  * http://SmartGYM.com/api/VJian/GetFingerPTAndMember
	  */
	 public function GetFingerPTAndMember($shop_id,$EmployeeCardNo,$MemberID,$varify_type=1,$has_txt=1){
	 	$this->before_method($shop_id);
	 	if(empty(input("EmployeeCardNo")||empty(input("MemberID")))){
	 		return $this->ajax_error("","0003","MemberID,EmployeeCardNo 为空");
	 	}else{
	 		if($varify_type==2){//获取人脸
	 			$e_card_no=input("EmployeeCardNo");
				$m_card_no=input("MemberCardNo");
				if(empty($m_card_no)){
					$card_nos=db("Interface")->where("shop_id=$shop_id and member_id='$MemberID'")->column("card_no");
					if(count($card_nos)!=1){
						return $this->ajax_error("","0003","MemberID 不唯一！");
					}else{
						$m_card_no=$card_nos[0];
					}	
				}
		 		$count=db("ModelDownRecord")->where("card_no in('$e_card_no','$m_card_no') and shop_id=$shop_id and is_down=0")->count(1);
				if($count<1){//没有更新
					if($has_txt==1){//本地有模板
			 			return $this->ajax_success(array("PTFinger"=>"","MemberFinger"=>""));
			 		}	
				}
	 		
	 			$pt_finger=db("FaceCode")->where("card_no='".input("EmployeeCardNo")."' and shop_id=$shop_id")->value("face1");
				if(empty($pt_finger)){
					return $this->ajax_error("","0003","pt未录入系统或未录入特征模板！");
				}
				$all_card_nos=explode(",", $m_card_no);
				if(count($all_card_nos)>1){
					$member_finger=db("FaceCode")->where(" shop_id=$shop_id and card_no in($m_card_no)")->value("face1");	
				}else{
					$member_finger=db("FaceCode")->where(" shop_id=$shop_id and card_no='$m_card_no'")->value("face1");	
				}
				if(empty($member_finger)){
					return $this->ajax_error("","0003","学员未录入系统或未录入特征模板！");
				}
				db("ModelDownRecord")->where("card_no in('$e_card_no','$m_card_no') and shop_id=$shop_id ")->setField(array("is_down"=>1));
	 		}else{//获取指纹
	 			$pt_finger=db("Interface")->where("card_no='".input("EmployeeCardNo")."' and shop_id=$shop_id")->value("fingerprint");
				if(empty($pt_finger)){
					return $this->ajax_error("","0003","pt未录入系统或未录入特征模板！");
				}
				$member_finger=db("Interface")->where("shop_id=$shop_id and member_id='".input("MemberID")."'")->value("fingerprint");
				if(empty($member_finger)){
					return $this->ajax_error("","0003","学员未录入系统或未录入特征模板！");
				}
	 		}
			return $this->ajax_success(array("PTFinger"=>$pt_finger,"MemberFinger"=>$member_finger));
	 	}
	}
	
	/**
	 * 开始上课
	 * http://SmartGYM.com/api/VJianApi/DoLessonStart
	 * */
	 public function DoLessonStart($EmployeeID,$MemberID,$ReservationID,$shop_id){
	 	$this->before_method($shop_id);
		$config_shopinfo=cache("ShopInfo_".$shop_id);
	 	if($config_shopinfo['api_version']=='vjian'){//微键系统
		 	if(empty($EmployeeID)||empty($MemberID)||empty($ReservationID)){
		 		return $this->ajax_error("","0003","EmployeeID,MemberID,ReservationID为空");
		 	}
		 	$result= http_post($this->url.self::DoLessonStart,['ClubID'=>$this->clubId,'EmployeeID'=>$EmployeeID,'MemberID'=>$MemberID,'ReservationID'=>$ReservationID]);
			if(!$result['flag']){
				return template_error($result['msg']);
			}
			$result=json_decode($result['data'],TRUE);
			if($result['Status']!=1){
				return $this->ajax_error('','0003',$result['Remark']);
			}else{
				return $this->ajax_success("成功");
			}
		}else{
			return $this->ajax_error("","0003","无效的api_version");
		}
	 }
	 /**
	 * 开始下课
	 * http://SmartGYM.com/api/AkV2Api/DoLessonEnd
	 * */
	 public function DoLessonEnd($EmployeeID,$MemberID,$ReservationID,$Number,$shop_id,$PrivateCardId){
	 	$this->before_method($shop_id);	
		$config_shopinfo=cache("ShopInfo_".$shop_id);
	 	if($config_shopinfo['api_version']=='vjian'){//微键系统
		 	if(empty($EmployeeID)||empty($MemberID)||empty($ReservationID)||empty($Number)||empty($PrivateCardId)){
		 		return $this->ajax_error("","0003","EmployeeID,MemberID,ReservationID,Number,PrivateCardId 为空");
		 	}
		 	$result= http_post($this->url.self::DoLessonEnd,['ClubID'=>$this->clubId,'ReservationID'=>$ReservationID,"PrivateCardId"=>$PrivateCardId,"EmployeeID"=>$EmployeeID,"MemberID"=>$MemberID,"Number"=>$Number]);
			if(!$result['flag']){
				return template_error($result['msg']);
			}
			$result=json_decode($result['data'],TRUE);
			if($result['Status']!=1){
				return $this->ajax_error('','0003',$result['Remark']);
			}else{
				return $this->ajax_success("成功");
			}
	 	}else{
			return $this->ajax_error("","0003","无效的api_version");
		}
	 	
	 }
	 
	  /**
	   * 私教卡
	   * http://SmartGYM.com/api/AkV2Api/CardList
	   * */
	 public function CardList($MemberID,$shop_id){
	 	$this->before_method($shop_id);	
		$config_shopinfo=cache("ShopInfo_".$shop_id);
	 	if($config_shopinfo['api_version']=='vjian'){//微键系统
		 	if(empty($MemberID)){
		 		return $this->ajax_error("","0003","MemberID");
		 	}
		 	$result= http_post($this->url.self::CardList,['ClubID'=>$this->clubId,'MemberID'=>$MemberID]);
			if(!$result['flag']){
				return template_error($result['msg']);
			}
			$result=json_decode($result['data'],TRUE);
			if($result['Status']!=1){
				return $this->ajax_error('','0003',$result['Remark']);
			}else{
				return $this->ajax_success($result['data']);
			}
	 	}else{
			return $this->ajax_error("","0003","无效的api_version");
		}
	 	
	 }
	
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
